package com.baizhi.action;

import com.baizhi.entity.Product;
import com.baizhi.service.ProductService;
import com.baizhi.service.impl.ProductServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

public class ProductAction {
    private int pageNum=1;
    private int pageSize=5;
    private int productId;
    private Product p;
    public void show() throws IOException {
        ProductService impl = new ProductServiceImpl();
//        System.out.println(pageNum);
        List<Product> list = impl.selectProducts(pageNum, pageSize);
        PageInfo<Product> pageInfo = new PageInfo<>(list, 10);
        int[] pages = pageInfo.getNavigatepageNums();

        HttpServletResponse resp = ServletActionContext.getResponse();
        resp.setCharacterEncoding("utf-8");
        PrintWriter pw = resp.getWriter();

        HashMap<String, Object> map = new HashMap<>();
        map.put("list",list);
        map.put("pages",pages);

        ObjectMapper objectMapper = new ObjectMapper();
        String mapJson = objectMapper.writeValueAsString(map);
        pw.print(mapJson);
        pw.flush();


    }
    public void deleteOne() throws IOException {
        ProductService impl = new ProductServiceImpl();
        HashMap<String, Object> map = new HashMap<>();

        HttpServletResponse resp = ServletActionContext.getResponse();
        resp.setCharacterEncoding("utf-8");
        PrintWriter pw = resp.getWriter();

        try {
            impl.deleteProductById(productId);
            map.put("status","success");
        } catch (Exception e) {
            map.put("status","failed");
            e.printStackTrace();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        String mapJson = objectMapper.writeValueAsString(map);
        pw.print(mapJson);
        pw.flush();
    }
    public void add() throws IOException {
        ProductService impl = new ProductServiceImpl();
        HashMap<String, Object> map = new HashMap<>();

        HttpServletResponse resp = ServletActionContext.getResponse();
        resp.setCharacterEncoding("utf-8");
        PrintWriter pw = resp.getWriter();

        try {
            impl.insertProduct(p);
            map.put("status","success");
        } catch (Exception e) {
            map.put("status","failed");
            e.printStackTrace();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        String mapJson = objectMapper.writeValueAsString(map);
        pw.print(mapJson);
        pw.flush();
    }
    public void showOne() throws IOException {
        ProductService impl = new ProductServiceImpl();
        HashMap<String, Object> map = new HashMap<>();

        HttpServletResponse resp = ServletActionContext.getResponse();
        resp.setCharacterEncoding("utf-8");
        PrintWriter pw = resp.getWriter();
        Product product = impl.selectProductById(productId);
        ObjectMapper objectMapper = new ObjectMapper();
        String productJson = objectMapper.writeValueAsString(product);
        pw.write(productJson);
        pw.flush();
    }

    public void updateOne() throws IOException {
        ProductService impl = new ProductServiceImpl();
        HashMap<String, Object> map = new HashMap<>();

        HttpServletResponse resp = ServletActionContext.getResponse();
        resp.setCharacterEncoding("utf-8");
        PrintWriter pw = resp.getWriter();

        try {
            impl.updateProduct(p);
            map.put("status","success");
        } catch (Exception e) {
            map.put("status","failed");
            e.printStackTrace();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        String mapJson = objectMapper.writeValueAsString(map);
        pw.print(mapJson);
        pw.flush();
    }
    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public Product getP() {
        return p;
    }

    public void setP(Product p) {
        this.p = p;
    }
}
